/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * GraphPanel.java
 *
 * Created on Dec 6, 2011, 7:15:11 PM
 */
package venndiagram;

/**
 * 
 * @author beckern
 */
import java.awt.*;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.awt.geom.*;
import java.io.File;
import java.io.OutputStream;
import java.util.ArrayList;
import javax.swing.JFileChooser;
import org.apache.commons.io.IOUtils;
import org.apache.xmlgraphics.java2d.ps.EPSDocumentGraphics2D;


public class GraphPanel extends javax.swing.JFrame {

    
    // Instance Variables
    private GraphData data; // Contains information on all three cirlces
    private Shape currentSection; // The user selected section
    private Shape a; // The max circle
    private Shape b; // The second to max circle
    private Shape c; // The smaller circle
    // Intersections
    private Shape ab; 
    private Shape bc;
    private Shape ac;
    private Shape abc;
    //Lists to hold colors
    private ArrayList<Shape> shapes = new ArrayList<Shape>(7);
    private ArrayList<Color> colors = new ArrayList<Color>(7);
    private ArrayList<String> setNames = new ArrayList<String>(7);
    private ArrayList<Integer> counts = new ArrayList<Integer>(7);
    private boolean started = false;
    
    /** Creates new form GraphPanel */
    public GraphPanel() {
        initComponents();
        // for testing purposes
        //drawVenn();
        
    }
    //Nicole
    /**
     * This constructor works with two lists of genes and takes a parameter
     * of a GraphData object containing the data needed to run this panel.
     * This constructor hides the controls pertaining to having a third list.
     * @param data 
     */
    public GraphPanel(GraphDataTwoSets data) {
        initComponents();
        // Set all input controls for three lists to be disabled
        colorCLabel.setEnabled(false);
        colorACLabel.setEnabled(false);
        colorBCLabel.setEnabled(false);
        listCColorButton.setEnabled(false);
        listACColorButton.setEnabled(false);
        listBCColorButton.setEnabled(false);
        //Create circles representing both sets
        this.data = data;
        a = new Ellipse2D.Double(data.maxCircle.getCenter().x - data.maxCircle.getRadius(),
                data.maxCircle.getCenter().y - data.maxCircle.getRadius(),2 * data.maxCircle.getRadius(),
                2 * data.maxCircle.getRadius());
        b = new Ellipse2D.Double(data.minCircle.getCenter().x - data.minCircle.getRadius(),
                data.minCircle.getCenter().y - data.minCircle.getRadius(),2 * data.minCircle.getRadius(),2 * data.minCircle.getRadius());
        // convert to areas for intersection
        Area aArea = new Area(a);
        Area bArea = new Area(b);
        aArea.intersect(bArea);
        ab = aArea;  
        
        counts = data.getCounts();
        
        //add the names of the set and fill the rest will null to know
        //not to draw them
        setNames.add(0, data.getMaxSetName());
        setNames.add(1, data.getMinSetName());
        setNames.add(2, null);
        setNames.add(3, null);
        setNames.add(4, null);
        setNames.add(5, null);
        setNames.add(6, null);
        setNames.add(7, null);
        
        setupLists();
        
        //set the added shapes and colors. 
        //Because drawingCanvas was made with Netbean's GUI builder, 
        //initializing the drawingCanvas as a VennCanvas was not possible.
        //So, in order to call the Venn Canvas methods, the object must
        //be type casted.
        ((VennCanvas)drawingCanvas).setColors(colors);
        ((VennCanvas)drawingCanvas).setShapes(shapes);
        ((VennCanvas)drawingCanvas).setNames(setNames);
        ((VennCanvas)drawingCanvas).setCounts(counts);
        ((VennCanvas)drawingCanvas).repaint();
    }
    
    /**
     * This constructor works with three lists of genes and takes a parameter
     * of a GraphData containing the data needed to run this panel.
     * @param data 
     */
    // Nicole
    public GraphPanel(GraphDataThreeSets data){
        initComponents();
        // Instantiate Instance Variables
        this.data = data;
        a = new Ellipse2D.Double(data.maxCircle.getCenter().x - data.maxCircle.getRadius(),
                data.maxCircle.getCenter().y - data.maxCircle.getRadius(),2 * data.maxCircle.getRadius(),
                2 * data.maxCircle.getRadius());
        b = new Ellipse2D.Double(data.midCircle.getCenter().x - data.midCircle.getRadius(),
                data.midCircle.getCenter().y - data.midCircle.getRadius(),2 * data.midCircle.getRadius(),
                2 * data.midCircle.getRadius());
        c = new Ellipse2D.Double(data.minCircle.getCenter().x - data.minCircle.getRadius(),
                data.minCircle.getCenter().y - data.minCircle.getRadius(),2 * data.minCircle.getRadius(),
                2 * data.minCircle.getRadius());
        
        // convert to areas for intersection
        Area aArea = new Area(a);
        Area bArea = new Area(b);
        Area cArea = new Area(c);
        // Create ab intersection through aArea
        aArea.intersect(bArea);
        ab = aArea; 
        // Create bc intersection through bArea
        bArea.intersect(cArea);
        bc = bArea;
        // Recreate aArea to represent a
        aArea = new Area(a);
        // Create ac intersection through cArea
        cArea.intersect(aArea);
        ac = cArea;
        // Recreate areas for each of the circles for intersection abc
        aArea = new Area(a);
        bArea = new Area(b);
        cArea = new Area(c);
        // intersect with b
        aArea.intersect(bArea);
        //intersect with c
        aArea.intersect(cArea);
        abc = aArea;
        
        counts = data.getSetCounts();
        
        //Add names of set to setNames list
        setNames.add(0, data.getMaxSetName());
        setNames.add(1, data.getMidSetName());
        setNames.add(2, data.getMinSetName());
        setNames.add(3, null);
        setNames.add(4, null);
        setNames.add(5, null);
        setNames.add(6, null);
        setNames.add(7, null);
        
        setupLists();
        
        //set the added shapes and colors. 
        //Because drawingCanvas was made with Netbean's GUI builder, 
        //initializing the drawingCanvas as a VennCanvas was not possible.
        //So, in order to call the Venn Canvas methods, the object must
        //be type casted.
        ((VennCanvas)drawingCanvas).setColors(colors);
        ((VennCanvas)drawingCanvas).setShapes(shapes);
        ((VennCanvas)drawingCanvas).setNames(setNames);
        ((VennCanvas)drawingCanvas).setCounts(counts);
        ((VennCanvas)drawingCanvas).repaint();
    }
    
    //Sets up the lists for drawing. This adds all of the shape objects,
    //including those that are null. VennCanvas checks if certain indices
    //hold null or not.
    private void setupLists(){
        //Add all of the shapes
        shapes.add(0, a);
        shapes.add(1, b);
        shapes.add(2, c);
        shapes.add(3, ab);
        shapes.add(4, bc);
        shapes.add(5, ac);
        shapes.add(6, abc);
        
        //Add initial colors
        colors.add(0, Color.red);
        colors.add(1, Color.green);
        colors.add(2, Color.blue);
        colors.add(3, Color.yellow);
        colors.add(4, Color.cyan);
        colors.add(5, Color.magenta);
        colors.add(6, Color.white);
    }

    
    /**
     * This method draws the Venn diagram to the canvas.
     * @throws IOException 
     */
//    private void drawVenn() {
//        drawingCanvas.repaint();
//    }
    
    @Override
    public void paint(Graphics g){
        super.paintComponents(g);
        drawingCanvas.repaint();
    }
    
    //Reset the color at a given index with a given color
    //And then replace the color in the drawingCanvas
    public void setColor(int index, Color color){
        colors.set(index, color);
        ((VennCanvas)drawingCanvas).setColors(colors);
        ((VennCanvas)drawingCanvas).repaint();
    }
    
    //Write postscript to a given file
    private void writePostScript(File outputFile) throws FileNotFoundException{
        //Setup output stream for writing to the file
        OutputStream outStream = new java.io.FileOutputStream(outputFile);
        outStream = new java.io.BufferedOutputStream(outStream);
        
        try{
            //Instantiate the EPSDocumentGraphics2D instance, which is 
            //a subclass of Java's Graphics object
            EPSDocumentGraphics2D g2d = new EPSDocumentGraphics2D(false);
            g2d.setGraphicContext(new org.apache.xmlgraphics.java2d.GraphicContext());

            //Set up the document size
            g2d.setupDocument(outStream, 476, 357);
            //send the EPSDocumentGraphics2D object into Venn Canvas's 
            //paint method as the graphics object. All of the Java2D calls
            //will draw to the document
            ((VennCanvas)drawingCanvas).paint(g2d);
            
            //Cleanup
            g2d.finish();
        }
        catch(Exception e){
            System.out.println(e.getMessage());
        } finally{
            IOUtils.closeQuietly(outStream);
        }
    }
    
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        posButtonsGroup = new javax.swing.ButtonGroup();
        mainPanel = new javax.swing.JPanel();
        colorALabel = new javax.swing.JLabel();
        listAColorButton = new javax.swing.JButton();
        colorBLabel = new javax.swing.JLabel();
        listBColorButton = new javax.swing.JButton();
        colorCLabel = new javax.swing.JLabel();
        listCColorButton = new javax.swing.JButton();
        colorABLabel = new javax.swing.JLabel();
        listABColorButton = new javax.swing.JButton();
        colorBCLabel = new javax.swing.JLabel();
        listBCColorButton = new javax.swing.JButton();
        colorACLabel = new javax.swing.JLabel();
        listACColorButton = new javax.swing.JButton();
        colorACLabel1 = new javax.swing.JLabel();
        listABCColorButton = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        jToggleButton1 = new javax.swing.JToggleButton();
        label1 = new java.awt.Label();
        jToggleButton2 = new javax.swing.JToggleButton();
        drawingCanvas = new VennCanvas(shapes, colors);
        jMenuBar1 = new javax.swing.JMenuBar();
        jMenu1 = new javax.swing.JMenu();
        jMenuItem1 = new javax.swing.JMenuItem();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setResizable(false);

        mainPanel.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Customize Diagram", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("DejaVu Serif", 0, 18))); // NOI18N

        colorALabel.setText("Choose Color for List 1:");

        listAColorButton.setForeground(new java.awt.Color(201, 83, 83));
        listAColorButton.setMaximumSize(new java.awt.Dimension(10, 10));
        listAColorButton.setMinimumSize(new java.awt.Dimension(10, 10));
        listAColorButton.setPreferredSize(new java.awt.Dimension(10, 10));
        listAColorButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                listAColorButtonActionPerformed(evt);
            }
        });

        colorBLabel.setText("Choose Color for List 2:");

        listBColorButton.setMaximumSize(new java.awt.Dimension(10, 10));
        listBColorButton.setMinimumSize(new java.awt.Dimension(10, 10));
        listBColorButton.setPreferredSize(new java.awt.Dimension(10, 10));
        listBColorButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                listBColorButtonActionPerformed(evt);
            }
        });

        colorCLabel.setText("Choose Color for List 3:");

        listCColorButton.setMaximumSize(new java.awt.Dimension(10, 10));
        listCColorButton.setMinimumSize(new java.awt.Dimension(10, 10));
        listCColorButton.setPreferredSize(new java.awt.Dimension(10, 10));
        listCColorButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                listCColorButtonActionPerformed(evt);
            }
        });

        colorABLabel.setText("Choose Color for Intersection of List 1 & 2:");

        listABColorButton.setForeground(new java.awt.Color(201, 83, 83));
        listABColorButton.setMaximumSize(new java.awt.Dimension(10, 10));
        listABColorButton.setMinimumSize(new java.awt.Dimension(10, 10));
        listABColorButton.setPreferredSize(new java.awt.Dimension(10, 10));
        listABColorButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                listABColorButtonActionPerformed(evt);
            }
        });

        colorBCLabel.setText("Choose Color for Intersection of List 1 & 3:");

        listBCColorButton.setForeground(new java.awt.Color(201, 83, 83));
        listBCColorButton.setMaximumSize(new java.awt.Dimension(10, 10));
        listBCColorButton.setMinimumSize(new java.awt.Dimension(10, 10));
        listBCColorButton.setPreferredSize(new java.awt.Dimension(10, 10));
        listBCColorButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                listBCColorButtonActionPerformed(evt);
            }
        });

        colorACLabel.setText("Choose Color for Intersection of List 2 & 3:");

        listACColorButton.setForeground(new java.awt.Color(201, 83, 83));
        listACColorButton.setMaximumSize(new java.awt.Dimension(10, 10));
        listACColorButton.setMinimumSize(new java.awt.Dimension(10, 10));
        listACColorButton.setPreferredSize(new java.awt.Dimension(10, 10));
        listACColorButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                listACColorButtonActionPerformed(evt);
            }
        });

        colorACLabel1.setText("Choose Color for Intersection of All Lists");

        listABCColorButton.setForeground(new java.awt.Color(201, 83, 83));
        listABCColorButton.setMaximumSize(new java.awt.Dimension(10, 10));
        listABCColorButton.setMinimumSize(new java.awt.Dimension(10, 10));
        listABCColorButton.setPreferredSize(new java.awt.Dimension(10, 10));
        listABCColorButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                listABCColorButtonActionPerformed(evt);
            }
        });

        jLabel1.setText("Text is");

        jToggleButton1.setText("On");
        jToggleButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jToggleButton1ActionPerformed(evt);
            }
        });

        label1.setText("Text In Center");

        jToggleButton2.setText("On");
        jToggleButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jToggleButton2ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout mainPanelLayout = new javax.swing.GroupLayout(mainPanel);
        mainPanel.setLayout(mainPanelLayout);
        mainPanelLayout.setHorizontalGroup(
            mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(mainPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(mainPanelLayout.createSequentialGroup()
                        .addComponent(colorALabel)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(listAColorButton, javax.swing.GroupLayout.PREFERRED_SIZE, 29, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(mainPanelLayout.createSequentialGroup()
                        .addComponent(colorBLabel)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(listBColorButton, javax.swing.GroupLayout.PREFERRED_SIZE, 29, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(mainPanelLayout.createSequentialGroup()
                        .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(colorABLabel)
                            .addGroup(mainPanelLayout.createSequentialGroup()
                                .addComponent(colorCLabel)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(listCColorButton, javax.swing.GroupLayout.PREFERRED_SIZE, 29, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addComponent(colorACLabel)
                            .addComponent(colorBCLabel))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(listACColorButton, javax.swing.GroupLayout.PREFERRED_SIZE, 29, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(listABColorButton, javax.swing.GroupLayout.PREFERRED_SIZE, 29, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(listBCColorButton, javax.swing.GroupLayout.PREFERRED_SIZE, 29, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(mainPanelLayout.createSequentialGroup()
                        .addComponent(colorACLabel1)
                        .addGap(18, 18, 18)
                        .addComponent(listABCColorButton, javax.swing.GroupLayout.PREFERRED_SIZE, 29, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(mainPanelLayout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jToggleButton1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(label1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jToggleButton2)))
                .addContainerGap(76, Short.MAX_VALUE))
        );
        mainPanelLayout.setVerticalGroup(
            mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(mainPanelLayout.createSequentialGroup()
                .addGap(11, 11, 11)
                .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(colorALabel, javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(listAColorButton, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 26, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(colorBLabel)
                    .addComponent(listBColorButton, javax.swing.GroupLayout.PREFERRED_SIZE, 26, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(mainPanelLayout.createSequentialGroup()
                        .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(listCColorButton, javax.swing.GroupLayout.PREFERRED_SIZE, 26, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGroup(mainPanelLayout.createSequentialGroup()
                                .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(listABColorButton, javax.swing.GroupLayout.PREFERRED_SIZE, 26, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addGroup(mainPanelLayout.createSequentialGroup()
                                        .addGap(7, 7, 7)
                                        .addComponent(colorABLabel)))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(colorCLabel)))
                        .addGap(18, 18, 18)
                        .addComponent(colorBCLabel))
                    .addComponent(listACColorButton, javax.swing.GroupLayout.PREFERRED_SIZE, 26, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(colorACLabel, javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(listBCColorButton, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 26, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(colorACLabel1)
                    .addComponent(listABCColorButton, javax.swing.GroupLayout.PREFERRED_SIZE, 26, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabel1)
                        .addComponent(jToggleButton1))
                    .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                        .addComponent(jToggleButton2)
                        .addComponent(label1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGap(61, 61, 61))
        );

        jMenu1.setText("File");

        jMenuItem1.setText("Save Diagram");
        jMenuItem1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem1ActionPerformed(evt);
            }
        });
        jMenu1.add(jMenuItem1);

        jMenuBar1.add(jMenu1);

        setJMenuBar(jMenuBar1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(drawingCanvas, javax.swing.GroupLayout.DEFAULT_SIZE, 476, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(mainPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addComponent(mainPanel, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 357, Short.MAX_VALUE)
                    .addComponent(drawingCanvas, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 357, Short.MAX_VALUE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void listAColorButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_listAColorButtonActionPerformed
        ColorChooser colorChooser = new ColorChooser(this, 0);
        colorChooser.setVisible(true);
    }//GEN-LAST:event_listAColorButtonActionPerformed

    private void listBColorButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_listBColorButtonActionPerformed
        // TODO add your handling code here:
        ColorChooser colorChooser = new ColorChooser(this, 1);
        colorChooser.setVisible(true);
    }//GEN-LAST:event_listBColorButtonActionPerformed

    private void listCColorButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_listCColorButtonActionPerformed
        // TODO add your handling code here:
        ColorChooser colorChooser = new ColorChooser(this, 2);
        colorChooser.setVisible(true);
    }//GEN-LAST:event_listCColorButtonActionPerformed

    private void listABColorButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_listABColorButtonActionPerformed
        // TODO add your handling code here:
        ColorChooser colorChooser = new ColorChooser(this, 3);
        colorChooser.setVisible(true);
    }//GEN-LAST:event_listABColorButtonActionPerformed

    private void listACColorButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_listACColorButtonActionPerformed
        // TODO add your handling code here:
        ColorChooser colorChooser = new ColorChooser(this, 5);
        colorChooser.setVisible(true);
    }//GEN-LAST:event_listACColorButtonActionPerformed

    private void listBCColorButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_listBCColorButtonActionPerformed
        // TODO add your handling code here:
        ColorChooser colorChooser = new ColorChooser(this, 4);
        colorChooser.setVisible(true);
    }//GEN-LAST:event_listBCColorButtonActionPerformed

    private void listABCColorButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_listABCColorButtonActionPerformed
        // TODO add your handling code here:
        ColorChooser colorChooser = new ColorChooser(this, 6);
        colorChooser.setVisible(true);
    }//GEN-LAST:event_listABCColorButtonActionPerformed

    private void jToggleButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jToggleButton1ActionPerformed
        // TODO add your handling code here:
        ((VennCanvas)drawingCanvas).toggleText();
        if(((VennCanvas)drawingCanvas).textIsOn()){
            jToggleButton1.setText("On");
        }
        else
            jToggleButton1.setText("Off");
    }//GEN-LAST:event_jToggleButton1ActionPerformed

    private void jMenuItem1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem1ActionPerformed
        // TODO add your handling code here:
        
        //setup file to save postscript document
        JFileChooser fileChooser = new JFileChooser();
        fileChooser.showSaveDialog(this);
        File outputFile = fileChooser.getSelectedFile();
        //if the filename given doesn't have an .eps file extension, 
        //append ".eps" to filename
        if(!(outputFile.getName().endsWith(".eps"))){
            outputFile = new File(outputFile.getPath()+".eps");
        }
        //write the postscript file
        try{
            writePostScript(outputFile);
        } catch(FileNotFoundException e){
            System.out.println(e.getMessage());
        }
        
        //write to postscript document
    }//GEN-LAST:event_jMenuItem1ActionPerformed

    private void jToggleButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jToggleButton2ActionPerformed
        // TODO add your handling code here:
        ((VennCanvas)drawingCanvas).toggleTextInCenter();
        if(((VennCanvas)drawingCanvas).textIsInCenter()){
            jToggleButton2.setText("On");
        }
        else
            jToggleButton2.setText("Off");
    }//GEN-LAST:event_jToggleButton2ActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(GraphPanel.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(GraphPanel.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(GraphPanel.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(GraphPanel.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                //Code for testing
                //
                //new GraphPanel(new GraphDataThreeSets("a", "b", "c", 30000, 15000, 10000, 0, 0, 0 ,0)).setVisible(true);
                new GraphPanel(new GraphDataTwoSets("a", "b", 30000, 15000, 0)).setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel colorABLabel;
    private javax.swing.JLabel colorACLabel;
    private javax.swing.JLabel colorACLabel1;
    private javax.swing.JLabel colorALabel;
    private javax.swing.JLabel colorBCLabel;
    private javax.swing.JLabel colorBLabel;
    private javax.swing.JLabel colorCLabel;
    protected java.awt.Canvas drawingCanvas;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JMenu jMenu1;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JMenuItem jMenuItem1;
    private javax.swing.JToggleButton jToggleButton1;
    private javax.swing.JToggleButton jToggleButton2;
    private java.awt.Label label1;
    private javax.swing.JButton listABCColorButton;
    private javax.swing.JButton listABColorButton;
    private javax.swing.JButton listACColorButton;
    private javax.swing.JButton listAColorButton;
    private javax.swing.JButton listBCColorButton;
    private javax.swing.JButton listBColorButton;
    private javax.swing.JButton listCColorButton;
    private javax.swing.JPanel mainPanel;
    private javax.swing.ButtonGroup posButtonsGroup;
    // End of variables declaration//GEN-END:variables
}
